package com.yanq.liancar.activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.RxBarTool;
import com.yanq.liancar.R;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SmallCLuckDrawActivity extends AppCompatActivity {
    @BindView(R.id.back_img)
    ImageView backImg;
    @BindView(R.id.tv_record)
    TextView tvRecord;
    @BindView(R.id.webView)
    WebView webView;
    //    @BindView(R.id.choujiang_num)
    //    TextView choujiangNum;
    //    @BindView(R.id.ll_guajiang)
    //    LinearLayout llGuajiang;

    private String url;
    private int where;          //1:优驾   2：停驶

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        RxBarTool.setStatusBarColor(this, R.color.main_color);
        setContentView(R.layout.activity_smallc_luckdraw);
        ButterKnife.bind(this);

        where=getIntent().getIntExtra("where",0);
        if (where==1){
            url = Config.s + "llc/LotteryForOptimalDriving/lotteryForGoodDriCar.jsp" + "?token=" + UserBean.getcToken(this);
        }else {
            url = Config.s + "/llc/lotteryForStopCar/lotteryForStopCar.jsp" + "?token=" + UserBean.getcToken(this);

        }

        setWebData(url);
    }

    @OnClick({R.id.back_img, R.id.tv_record})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_img:
                finish();
                break;
            case R.id.tv_record:
                //抽奖记录
                RxActivityTool.skipActivity(this, LotteryRecordActivity.class);
                break;
        }
    }

    //    @OnClick(R.id.ll_guajiang)
    //    public void onViewClicked() {
    //        myDialog();
    //
    //    }

    @SuppressLint("SetJavaScriptEnabled")
    private void setWebData(String url) {

        ToastUtil.showProgressDialog(this);
        WebSettings setting = webView.getSettings();
        setting.setJavaScriptEnabled(true);
        setting.setBlockNetworkImage(false);
        setting.setSupportZoom(true);
        setting.setUseWideViewPort(true);
        setting.setAllowFileAccess(true);
        setting.setCacheMode(WebSettings.LOAD_NO_CACHE);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            setting.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

        webView.loadUrl(url);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                ToastUtil.dismissProgressDialog();
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                ToastUtil.dismissProgressDialog();
            }
        });

        webView.addJavascriptInterface(new JSInterface(),"Android");
    }


    private void myDialog() {
        final Dialog bottomDialog = new Dialog(this);
        View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_city, null);
        bottomDialog.setContentView(contentView);
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) contentView.getLayoutParams();
        //        params.width = getResources().getDisplayMetrics().widthPixels - CommonUtils.dp2px(getContext(), 80f);
        contentView.setLayoutParams(params);
        bottomDialog.getWindow().setGravity(Gravity.CENTER_HORIZONTAL);
        bottomDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation2);
        bottomDialog.setCanceledOnTouchOutside(true);
        bottomDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        bottomDialog.show();

        //点击领取
        //        TextView tv_cancle = bottomDialog.findViewById(R.id.tv_Receive);
        //        tv_cancle.setOnClickListener(new View.OnClickListener() {
        //            @Override
        //            public void onClick(View view) {
        //
        //                bottomDialog.dismiss();
        //            }
        //        });

    }

    class JSInterface {
        @JavascriptInterface
        public void myfinsh(){
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        webView.stopLoading();
        webView.removeAllViews();
        webView.destroy();
        webView = null;

    }
}
